﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.SessionState;



namespace NJL.WebUsers
{
    /// <summary>
    /// Handler 的摘要说明
    /// </summary>
    public class Handler : IHttpHandler, IRequiresSessionState
    {
        NJL.BLL.NJL_MYFRIEND bllFriend = new BLL.NJL_MYFRIEND();

        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request.QueryString["UserName"] != null)
            {
                string userName = context.Request.QueryString["UserName"].ToString();
                if (bllUser.GetUserNameByName(userName))
                {
                    context.Response.Write("NO");
                }
            }
            else if (context.Request.QueryString["QQ"] != null)
            {
                string qq = context.Request.QueryString["QQ"].ToString();
                if (bllUser.GetQQByQQ(qq))
                {
                    context.Response.Write("NOQQ");
                }
            }
            else if (context.Request.QueryString["CellPhoneNumber"] != null)
            {
                string CellPhoneNumber = context.Request.QueryString["CellPhoneNumber"].ToString();
                if (bllUser.GetCellPhoneByCellPhoneNumber(CellPhoneNumber))
                {
                    context.Response.Write("NOCellPhoneNumber");
                }
            }
            else if (context.Request.QueryString["Email"] != null)
            {
                string txtEmail = context.Request.QueryString["Email"].ToString();
                if (bllUser.GetEmailPhoneByEmail(txtEmail))
                {
                    context.Response.Write("NOEmail");
                }
            }
            else if (context.Request.QueryString["userid"] != null)
            {
                if (context.Session["userid"] == null)
                {
                    context.Response.Write("nologin");
                    return;
                }
                NJL.Model.NJL_MYFRIEND modelFriend;
                string userId = context.Request.QueryString["userid"].ToString();
                DataSet ds = bllFriend.GetList("NJL_MYFRIEND.FRIEND_USER_ID=" + userId + " and NJL_MYFRIEND.NJL_USER_ID=" + context.Session["userid"].ToString());
                if (ds.Tables[0].Rows.Count > 0)
                {
                    context.Response.Write("false");
                }
                else
                {
                    modelFriend = new Model.NJL_MYFRIEND();
                    modelFriend.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                    modelFriend.FRIEND_USER_ID = userId;
                    modelFriend.NJL_USER_ID = context.Session["userid"].ToString();
                    modelFriend.CREATE_DATE_TIME = DateTime.Now;
                    modelFriend.CREATE_USER_ID = context.Session["userid"].ToString();
                    modelFriend.CREATE_USER_NAME = context.Session["username"].ToString();
                    if (bllFriend.Add(modelFriend))
                    {

                        context.Response.Write("true");
                        string message;
                        if (new BasePageVisitor().HandleIntegral(context.Session["userid"].ToString(), "4", out message))
                        {

                        }
                    }
                }

            }
            else if (context.Request.QueryString["deleteFriend"] != null)
            {
                string userid = context.Request.QueryString["deleteFriend"].ToString();
                DataSet ds = bllFriend.GetList("NJL_MYFRIEND.FRIEND_USER_ID='" + userid + "' and NJL_MYFRIEND.NJL_USER_ID='" + context.Session["userid"].ToString() + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (bllFriend.Delete(ds.Tables[0].Rows[0]["ID2"].ToString()))
                    {

                        context.Response.Write("true");
                        string message;
                        if (new BasePageVisitor().HandleIntegral(context.Session["userid"].ToString(), "-4", out message))
                        {

                        }
                    }
                }

            }
            else if (context.Request.Form["sendUserid"] != null && context.Request.Form["content"] != null)
            {
                string sendUserid = context.Request.Form["sendUserid"].ToString();
                string content = context.Request.Form["content"].ToString();
                NJL.BLL.NJL_MESSAGES bllMessage = new BLL.NJL_MESSAGES();
                Model.NJL_MESSAGES modelMessage = new Model.NJL_MESSAGES();
                modelMessage.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                modelMessage.CREATE_USER_ID = context.Session["userid"].ToString();
                modelMessage.CREATE_USER_NAME = context.Session["username"].ToString();
                modelMessage.TO_USER_ID = sendUserid;
                modelMessage.MESSAGE_CONTENT = content;
                modelMessage.CREATE_DATE_TIME = DateTime.Now;
                modelMessage.MESSAGE_TYPE = 2; //2代表个人
                if (bllMessage.Add(modelMessage))
                {
                    context.Response.Write("true");
                }
            }
            else if (context.Request.QueryString["companyEvaluation"] != null && context.Request.QueryString["content"] != null && context.Request.QueryString["typecode"] != null)
            {


                NJL.BLL.NJL_COMPANY_EVALUATION bll = new BLL.NJL_COMPANY_EVALUATION();
                NJL.Model.NJL_COMPANY_EVALUATION model = new Model.NJL_COMPANY_EVALUATION();
                string companyId = context.Request.QueryString["companyEvaluation"].ToString();
                string commentContent = context.Request.QueryString["content"].ToString();
                model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                model.NJL_COMPANY_ID = companyId;
                model.EVALUATION_TYPE_CODE = Convert.ToInt32(context.Request.QueryString["typecode"]);
                if (context.Session["userid"] != null && context.Session["username"] != null)
                {
                    model.CREATE_USER_ID = context.Session["userid"].ToString();
                    model.CREATE_USER_NAME = context.Session["username"].ToString();
                }
                model.CREATE_DATE_TIME = DateTime.Now;
                model.COMPANY_EVALUATION = commentContent;
                if (bll.Add(model))
                {
                    context.Response.Write(commentContent);
                }

            }
            else if (context.Request.Form["city"] != null)
            {
                DataSet ds;
                string cityId = context.Request.Form["city"];
                NJL.BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();
                NJL.Model.NJL_CS_AREA modelArea = bllArea.GetModel(cityId);
                NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
                int njxy;
                int djsz;
                int jdms;
                int dfts;
                if (cityId == "110000" || cityId == "120000" || cityId == "310000" || cityId == "500000")
                {
                    ds = bllCompany.GetAllList("TYPE_CODE='1' and NJL_SYS_BASE_ID='" + cityId + "'");
                    njxy = ds.Tables[0].Rows.Count;
                    ds = bllCompany.GetAllList("TYPE_CODE='2' and NJL_SYS_BASE_ID='" + cityId + "'");
                    djsz = ds.Tables[0].Rows.Count;
                    NJL.BLL.NJL_ATTRACTION bllAttr = new BLL.NJL_ATTRACTION();
                    ds = bllAttr.GetAllList("NJL_SYS_BASE_ID='" + cityId + "'");
                    jdms = ds.Tables[0].Rows.Count;
                    NJL.BLL.NJL_LOCAL_COLOR bllLocal = new BLL.NJL_LOCAL_COLOR();
                    ds = bllLocal.GetAllList("NJL_SYS_BASE_ID='" + cityId + "'");
                    dfts = ds.Tables[0].Rows.Count;
                }
                else
                {
                    ds = bllCompany.GetAllList("TYPE_CODE='1' and AREA_CODE='" + cityId + "'");
                    njxy = ds.Tables[0].Rows.Count;
                    ds = bllCompany.GetAllList("TYPE_CODE='2' and AREA_CODE='" + cityId + "'");
                    djsz = ds.Tables[0].Rows.Count;
                    NJL.BLL.NJL_ATTRACTION bllAttr = new BLL.NJL_ATTRACTION();
                    ds = bllAttr.GetAllList("AREA_CODE='" + cityId + "'");
                    jdms = ds.Tables[0].Rows.Count;
                    NJL.BLL.NJL_LOCAL_COLOR bllLocal = new BLL.NJL_LOCAL_COLOR();
                    ds = bllLocal.GetAllList("AREA_CODE='" + cityId + "'");
                    dfts = ds.Tables[0].Rows.Count;
                }
                context.Response.Write(modelArea.NJL_DESCRIPTION + "," + modelArea.AREA_NAME + "," + njxy + "," + djsz + "," + jdms + "," + dfts);
            }
            else if (context.Request.QueryString["validateCode"] != null)
            {
                string validateCode = context.Request.QueryString["validateCode"];
                if (context.Session["CheckCode"] != null && validateCode.ToLower() == context.Session["CheckCode"].ToString().ToLower())
                {
                    context.Response.Write("Ture");
                }
                else
                {
                    context.Response.Write("False");
                }
            }
            else if (context.Request.QueryString["collection"] != null && context.Request.QueryString["collType"] != null)
            {
                if (context.Session["userid"] == null && context.Session["username"] == null)
                {
                    context.Response.Write("NoSession");
                }
                else
                {
                    NJL.BLL.NJL_MY_COLLECTION bll = new BLL.NJL_MY_COLLECTION();
                    NJL.Model.NJL_MY_COLLECTION model = new Model.NJL_MY_COLLECTION();
                    string collectionID = context.Request.QueryString["collection"];
                    string collType = context.Request.QueryString["collType"];
                    DataSet ds = bll.GetList("NJL_USER_ID='" + context.Session["userid"].ToString() + "' and TARGET_ID='" + collectionID + "' and COLLECTION_TYPE='" + collType + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        context.Response.Write("False");
                    }
                    else
                    {
                        model.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                        model.CREATE_USER_ID = context.Session["userid"].ToString();
                        model.CREATE_USER_NAME = context.Session["username"].ToString();
                        model.CREATE_DATE_TIME = DateTime.Now;
                        model.TARGET_ID = collectionID;
                        model.NJL_USER_ID = context.Session["userid"].ToString();
                        model.COLLECTION_TYPE = collType;
                        if (bll.Add(model))
                        {
                            ds = bll.GetList("TARGET_ID='" + collectionID + "' and COLLECTION_TYPE='" + collType + "'");
                            context.Response.Write(ds.Tables[0].Rows.Count.ToString());
                        }
                    }
                }
            }
            else if (context.Request.QueryString["companyid"] != null && context.Request.QueryString["typecode"] != null)
            {
                NJL.BLL.NJL_COMPANY_EVALUATION bll = new BLL.NJL_COMPANY_EVALUATION();

                IList<Model.NJL_COMPANY_EVALUATION> modelList = bll.GetModelList("NJL_COMPANY_ID='" + context.Request.QueryString["companyid"] + "' AND EVALUATION_TYPE_CODE=" + context.Request.QueryString["typecode"].ToString());

                context.Response.Write(modelList.Count.ToString());
            }
            else if (context.Request.QueryString["sessionUrl"] != null)
            {
                string url = context.Request.QueryString["sessionUrl"];
                //context.Session["url"] = url;
                context.Session.Add("url", url);
            }
            else if (context.Request.QueryString["SecondsessionUrl"] != null)
            {
                string url = context.Request.QueryString["SecondsessionUrl"];
                //context.Session["secondurl"] = url;
                context.Session.Add("secondurl", url);
            }
            else if (context.Request.QueryString["supportobjectid"] != null && context.Request.QueryString["supporttypecode"] != null && context.Request.QueryString["option"] == "getcount")
            {
                NJL.BLL.NJL_REG_SUPPORT bll = new BLL.NJL_REG_SUPPORT();
                string strObjectID = context.Request.QueryString["supportobjectid"];
                string strSupportType = context.Request.QueryString["supporttypecode"];//1：农家乐，2：景点，3：地方特色

                IList<Model.NJL_REG_SUPPORT> list = bll.GetModelList(" SUPPORT_OBJECT_TYPE=" + strSupportType + " AND SUPPORT_OBJECT_ID='" + strObjectID + "' ");
                context.Response.Write(list.Count.ToString());
            }
            else if (context.Request.QueryString["supportobjectid"] != null && context.Request.QueryString["supporttypecode"] != null)
            {
                NJL.BLL.NJL_REG_SUPPORT bll = new BLL.NJL_REG_SUPPORT();

                string strObjectID = context.Request.QueryString["supportobjectid"];
                string strSupportType = context.Request.QueryString["supporttypecode"];//1：农家乐，2：景点，3：地方特色
                string strIP = context.Request.UserHostAddress;//客户端IP
                DateTime dtime = System.DateTime.Now;

                //
                IList<Model.NJL_REG_SUPPORT> list = bll.GetModelList(1, " SUPPORT_OBJECT_TYPE=" + strSupportType + " AND SUPPORT_OBJECT_ID='" + strObjectID + "' AND IP='" + strIP + "' ", "CREATE_DATE_TIME DESC");

                if (list.Count == 1)
                {
                    if ((dtime - list[0].CREATE_DATE_TIME.Value).TotalMinutes < 5)
                    {
                        context.Response.Write("NO");
                    }
                    else
                    {
                        NJL.Model.NJL_REG_SUPPORT model = new Model.NJL_REG_SUPPORT();

                        model.ID = dtime.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                        model.SUPPORT_OBJECT_TYPE = Convert.ToInt32(strSupportType);
                        model.SUPPORT_OBJECT_ID = strObjectID;
                        model.IP = strIP;
                        model.CREATE_DATE_TIME = dtime;

                        bll.Add(model);

                        if (strSupportType == "1")
                        {
                            NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
                            NJL.Model.NJL_COMPANY modelCompany = bllCompany.GetModel(strObjectID);
                            modelCompany.SUPPORT_COUNT = bll.GetModelList(" SUPPORT_OBJECT_TYPE=" + strSupportType + " AND SUPPORT_OBJECT_ID='" + strObjectID + "' ").Count;
                            bllCompany.Update(modelCompany);
                        }
                        else if (strSupportType == "2")
                        {
                            NJL.BLL.NJL_ATTRACTION bllAttr = new BLL.NJL_ATTRACTION();
                            NJL.Model.NJL_ATTRACTION modelAttr = bllAttr.GetModel(strObjectID);
                            modelAttr.SUPPORT_COUNT = bll.GetModelList(" SUPPORT_OBJECT_TYPE=" + strSupportType + " AND SUPPORT_OBJECT_ID='" + strObjectID + "' ").Count;
                            bllAttr.Update(modelAttr);
                        }

                        context.Response.Write("OK");
                    }
                }
                else
                {
                    NJL.Model.NJL_REG_SUPPORT model = new Model.NJL_REG_SUPPORT();

                    model.ID = dtime.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
                    model.SUPPORT_OBJECT_TYPE = Convert.ToInt32(strSupportType);
                    model.SUPPORT_OBJECT_ID = strObjectID;
                    model.IP = strIP;
                    model.CREATE_DATE_TIME = dtime;

                    bll.Add(model);

                    if (strSupportType == "1")
                    {
                        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
                        NJL.Model.NJL_COMPANY modelCompany = bllCompany.GetModel(strObjectID);
                        modelCompany.SUPPORT_COUNT = bll.GetModelList(" SUPPORT_OBJECT_TYPE=" + strSupportType + " AND SUPPORT_OBJECT_ID='" + strObjectID + "' ").Count;
                        bllCompany.Update(modelCompany);
                    }
                    else if (strSupportType == "2")
                    {
                        NJL.BLL.NJL_ATTRACTION bllAttr = new BLL.NJL_ATTRACTION();
                        NJL.Model.NJL_ATTRACTION modelAttr = bllAttr.GetModel(strObjectID);
                        modelAttr.SUPPORT_COUNT = bll.GetModelList(" SUPPORT_OBJECT_TYPE=" + strSupportType + " AND SUPPORT_OBJECT_ID='" + strObjectID + "' ").Count;
                        bllAttr.Update(modelAttr);
                    }
                    context.Response.Write("OK");
                }

                context.Response.Write("");
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}